﻿package  
{
	/**
	 * ...
	 * @author maximov
	 */
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.media.Camera;
    import flash.media.Video;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import org.libspark.flartoolkit.core.FLARCode;
    import org.libspark.flartoolkit.core.param.FLARParam;
    import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
    import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
	
	   
	public class Flar
	{
		 private var param:FLARParam;

         private var code:FLARCode;
         private var raster:FLARRgbRaster_BitmapData;
         private var detector:FLARSingleMarkerDetector;
		 public var timer:Timer;
		 public var detect_timer:Timer;

         private var webcam:Camera;
         private var video:Video;
         private var capture:Bitmap;
         
         [Embed(source = "assets/camera_para.dat", mimeType = "application/octet-stream")]
         private var CParam:Class;
         [Embed(source = "assets/flarlogo.pat", mimeType = "application/octet-stream")]
         private var CodeData:Class;

		 public var result:FLARTransMatResult;
         public var MarkerNode:FLARBaseNode;
		 public var camera3d:FLARCamera3D;
		 public var layer:Sprite;
		 
		 public var detected:Boolean = false;
		 
		 public const capture_fps:Number = 30;
		 public const detect_fps:Number = 10;
		 
		 public var frame_width:int = 320;
		 public var frame_height:int = 240;
		public function Flar(width:Number, height:Number) 
		{
			trace(width);
			trace(height);
			 //setup webcam
			frame_height = height/4;
			frame_width = width/4;
			
                        webcam = Camera.getCamera();
                        if (!webcam) {
                                throw new Error("No webcam found! Can do nothing without it :`( " );
                        }
						webcam.setMode(frame_width, frame_height, 60,false );
						//frame_width = webcam.width;
						//frame_height = webcam.height;
						
			
					var codeFile:ByteArray = new CodeData() as ByteArray;

                        
					param = new FLARParam();
                        param.loadARParam(new CParam());
                        param.changeScreenSize(frame_width, frame_height);
                        code = new FLARCode(16, 16);
						
                        code.loadARPatt(codeFile.readUTF());

                        
						
                        video = new Video(frame_width, frame_height);
                        video.attachCamera(webcam);
						
						capture = new Bitmap(new BitmapData(frame_width, frame_height, false, 0), PixelSnapping.AUTO, false);
						
						
                        // setup ARToolkit
                        raster = new FLARRgbRaster_BitmapData(capture.bitmapData);
                        detector = new FLARSingleMarkerDetector(param, code, 80);
                        detector.setContinueMode(true);

						layer=new Sprite();
						capture.width = frame_width;
						capture.height = frame_height;
						
						layer.addChild(video);
					
						result = new FLARTransMatResult();
						MarkerNode=new FLARBaseNode(FLARBaseNode.AXIS_MODE_PV3D)
                        camera3d = new FLARCamera3D(param);
						camera3d.useCulling = true;
						/*
						timer = new Timer(1000.0 / capture_fps);
						timer.addEventListener(TimerEvent.TIMER, get_capture);
						timer.start();
						*/
						/*detect_timer = new Timer(1000.0 / detect_fps);
						timer.addEventListener(TimerEvent.TIMER, detect);
						detect_timer.start();
						*/

		}
		public function scale_to(width:Number, height:Number):void
		{
			layer.scaleX = -width/frame_width;
			layer.scaleY = height/frame_height;
			layer.x = width;
		}
		
		public function get_capture(E:Event=null):void
		{
			
			capture.bitmapData.draw(video);
            //return detected;
			detected = detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.45;
			if (detected)
			{
				 detector.getTransformMatrix(result);
                 MarkerNode.setTransformMatrix(result);
			}
			
		}
		public function detect(E:Event=null):Boolean
		{
		 detected = detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.45;
			if (detected)
			{
				 detector.getTransformMatrix(result);
                 MarkerNode.setTransformMatrix(result);
			}
			return detected;
		}	
		
	}

}